import gameConfig from '../../engines/configs/gameConfig';
import O1HD, { managers, services } from '../../engines/gameEngine';
import { REWARD_TYPE } from '../../engines/services/userContent/userInterface';
import PageBase from '../Base/PageBase';
import { GAME_EVENT } from '../Enum';
import { ILosePagePrams, INormalLevelStruct } from '../Interface';
import mainRoot from '../MainRoot';

const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@disallowMultiple
@menu('游戏页面/失败结算页')
export default class WinPage extends PageBase {
    @property({ type: cc.Label, displayName: "过关文字显示" })
    private winLabel: cc.Label = null;
    @property({ type: cc.Sprite, displayName: "过关显示图片" })
    private showImg: cc.Sprite = null;
    @property({ type: cc.Button, displayName: "分享按钮" })
    private shareBtn: cc.Button = null;
    @property({ type: cc.Node, displayName: "广告标识" })
    private adIcon: cc.Node = null;

    @property({type:cc.Label,tooltip:"需要的体力数量"})
    powerLb:cc.Label = null;

    private tipsContent: string = '';

    async OnPageOpen(prams: ILosePagePrams) {
        this.tipsContent = prams.tips;
        let level:number = prams.index;
        let labelNode = this.node.getChildByName("Content").getChildByName("Label"+level);
        labelNode && (labelNode.active = true);
        // this.winLabel.string = prams.loseText;
        super.OnPageOpen(prams);
        services().miniGameSrv.getAppApiInstance().gameRecordStop();
        O1HD.on(GAME_EVENT.ON_TIPS_GOT, this.OnTipsGot.bind(this));

        let levelNum = mainRoot.LevelSystem.GetCurrLevel(this.m_mode);
        let realNum = mainRoot.LevelSystem.realNum(levelNum, this.m_mode);
        const content: INormalLevelStruct = mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, realNum);
        let subPower:number = content["subPower"] || 1;
        this.powerLb.string = `-${subPower}`;

        // this.OnTipsGot();
        // this.adIcon.active = !mainRoot.GotTips;
        // this.adIcon.parent.active =false
    }

    async OnRestartBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        // if (O1HD.getServices().userSrv.getPower() - gameConfig.MODE_POWER[this.m_mode] < 0) {
        //     mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER);
        //     return false;
        // }
        let levelNum = mainRoot.LevelSystem.GetCurrLevel(this.m_mode);
        let realNum = mainRoot.LevelSystem.realNum(levelNum, this.m_mode);
        const content: INormalLevelStruct = mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, realNum);
        let subPower:number = content["subPower"] || 1;
        if(O1HD.getServices().userSrv.getPower()- subPower < 0){
            mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        O1HD.getManagers().soundMgr.playSound('mainSound/inLevel').then();
        await mainRoot.UISystem.CloseAllPage();
        services().userSrv.setPower(-subPower, true, true);
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode);
        mainRoot.GotTips = false
        mainRoot.GotAnswer = false;
        this.ClosePage();
    }

    OnTipsGot() {
        // this.adIcon.active = !mainRoot.GotTips;
    }

    OnTipsBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();

        if (mainRoot.GotTips) {
            O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
            return;
        }
        cc.director.pause()
        services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
            // mainRoot.GotTips = true;
            O1HD.emit(GAME_EVENT.ON_BTN_TIPS);
            cc.director.resume()
            // mainRoot.UISystem.OpenPage(PAGE.TIPSPAGE,tipsPrams);
        }, () => { cc.director.resume()}, () => {cc.director.resume() }, "看视频获取提示");
    }

    OnShareBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        if (mainRoot.platName == "novelapp") {
            return;
        }

        // if(services().ossSrv.getPlatform()=="ks"){
        //     this.shareBtn.node.active = false;
        // }
        services().miniGameSrv.getAppApiInstance().shareGameRecordVideo(() => {
            // this.shareBtn.node.active = false;
            // this.node.getChildByName("BtnRestart").setPosition(0,0)  //临时用一下 后面改oss资源
        });

    }
}